/**
 * Copyright 2015 Qiyi Inc. All rights reserved.
 *
 * @file:   Program.js
 * @path:   js-src/album/
 * @author: karkarrotxu@qiyi.com
 * @date:   2016-03-01
 */
qui(
    function (require, exports, module) {
        VOD.Task = (function () {
            var settings = VOD.Dao.settings;
            var dotData = VOD.Dao.getDotData();
            var global = VOD.Config.Global;

            var g_mutex = 0; // 防止数据不一致
            var g_ocrList = []; // ocr 识别出的title数据
            var g_aMutex = 0; // 防止数据不一致
            var g_acrList = []; // ocr 识别出的title数据

            var getData = function () {
                $('#addInfo,#d_addInfo').removeAttr('disabled').html('提交');

                if (settings.isOver == 0) {
                    return false;
                }
                var listUrl = dotData.listUrl;
                if (settings.taskid > 0 && settings.isStart) {
                    settings.isOver = 0
                    $.ajax({
                        type: "get",
                        url: listUrl,
                        data: {
                            'key': settings.taskid,
                            'from_client': true,
                            'index': settings.lastImg,
                            'type': 1,
                            'offset': 100
                        },
                        success: function (data) {
                            settings.isOver = 1 //防止并发
                            if (data.list.length) {
                                var li = '';
                                var sty = VOD.Config.Global.picWH;
                                var thisFpTime = "";
                                $.each(data.list, function (i, value) {
                                    var fp_time = value.fp;
                                    VOD.Config.Global.fp_pos = fp_time.lastIndexOf('/') + 1;
                                    fp_time = fp_time.substr(VOD.Config.Global.fp_pos, 10);
                                    if (!settings.isSetFpTime) {
                                        settings.firstFpTime = parseInt(fp_time);
                                        thisFpTime = '00:00:00';
                                    } else {
                                        var tmpFpTime = (parseInt(fp_time) - settings.firstFpTime);
                                        thisFpTime = VOD.Util.getDataByNum(tmpFpTime);
                                    }
                                    //////////////////////////////////////////////////////////////////////////////////////////

                                    var s = '<div class="text-center relative ' + thisFpTime.replace(/:/g, "-") + ' ' + value.id + '-jump" id="cate-' + value.vid + '">'
                                        + '<img class="fragment-img" id="img-' + value.id + '" src="' + value.fp + '"  ondblclick="VOD.Util.dl_img_click(event, \'' + value.id + '\');" onerror="VOD.Player.nofind(\'' + value.fp + '\');"' + sty + ' />'
                                        + '<div class="absolute ' + settings.fpTime + '">' + thisFpTime + '</div><br/>'
                                        + '<span class="pid">' + value.id + '</span>'
                                        + '</div>';
                                    //////////////////////////////////////////////////////////////////////////////////
                                    if ( (global.smallPic&&settings.lb % settings.linePicNum == 0) || (!global.smallPic &&settings.lb % settings.linePicNum == 0) ) {
                                        s += '<div class="clearfix breakWord">&nbsp;</div>';
                                    }

                                    $('#makedImg').append(s);
                                    //出来第一张图的时候计算几张图片一行
                                    if (!settings.isSetFpTime) {
                                        VOD.Util.getLineWidth();
                                        settings.isSetFpTime = true;
                                    }
                                    li = value.id + 1;
                                    settings.lb++;
                                });

                                if (li) {
                                    settings.lastImg = li;
                                    $('#picNum').html(li);
                                }
                                if ($('#autoRunPic').prop('checked')) {
                                    var sTop = $('#makedImg')[0].scrollHeight;
                                    $('#makedImg').scrollTop(sTop);
                                }

                            } else {
                                //请求无数据的次数
                                if (settings.noneDataNum > 5) {
                                    console.log('请求错误超过5次');
                                } else {
                                    settings.noneDataNum++
                                }
                            }
                        },
                        dataType: 'json',
                        error: function () {
                            settings.isOver = 1;
                            //alert('图片请求错误,再次尝试请求中!');
                            VOD.Plugin.tips("error", '图片请求错误,再次尝试请求中!');
                        }
                    });
                } else {
                    console.log("null taskid")
                }
            }
            var getSty2 = function () {
                if (settings.taskid && settings.isStart) {

                    $.get(dotData.sty2Url , {
                        'key': settings.taskid,
                        'index': 0,
                        'offset': 100,
                        'from_client': true
                    }, function (data) {
                        //adLibList = [];
                        global.  sty2Data = data;
                        var ad_lib_list = data.list.splice(settings.last_i);
                        $.each(ad_lib_list, function (i, item) {
                            if (item.video_list[item.video_list.length - 1] > parseInt($("#picNum").text(), 10) - 1 || item.video_list.length === 0) {
                                return;
                            }
                            settings.last_i++;

                            var drawUnderline = function (id, color) {
                                try {
                                    var html = '<div style="left:50%;position:absolute;width:50%;height:3px;border-bottom:3px solid ' + color + ';"></div>';
                                    $('#cate-' + id).append(html);
                                }
                                catch (e) {
                                    console.log(e.message);
                                }
                            }

                            if (item.type == global.adLibHead || item.type ==  global.adLibTail) {
                                $.each(item.video_list, function (j, id) {
                                    if ($('#cate-' + id).attr("style") != undefined)
                                        drawUnderline(id, 'green');
                                    else
                                        $('#cate-' + id).css('border-bottom', '3px solid green');
                                });
                            }
                            else if (item.type == VOD.CONFIG("adLib")) {
                                if (item.video_list.length > 0){
                                    settings.adLibList.push({s: item.video_list[0], e: item.video_list[item.video_list.length - 1]});
                                    }

                            //    var i = 0;
                            //    var la = item.video_list.length;
                                $.each(item.video_list, function (j, id) {
                                    if ($('#cate-' + id).attr("style") != undefined)
                                        drawUnderline(id, 'red');
                                    else {
                                        $('#cate-' + id).css('border-bottom', '3px solid red');
                                    }
                                   // i++;
                                });
                            }
                        });
                    }, 'json');
                }
            }

            function getOcr() {
                g_mutex = 1;
                var tmp = [];
                if (settings.taskid && settings.isStart) {
                    $.get(dotData.ocrUrl, {
                        'key' : settings.taskid,
                        'index' : 0,
                        'type' : 1,
                        'from_client' : true
                    }, function (data) {

                       var ocr_list = data.list;
                        var len = ocr_list.length;
                    //   var g_ocrIndex = len;
                        if (len) {
                            for (var i in ocr_list) {
                                if (ocr_list[i]['text'] == '' || ocr_list[i]['text'].length == 1 || ocr_list[i]['video_list'] == '')
                                    continue;

                                for (var j in ocr_list[i]['video_list']) {
                                    ocr_list[i]['video_list'][j] = ocr_list[i]['video_list'][j] - 1;
                                }

                                var tmp2 = [];

                                if (ocr_list[i]['video_list'].length == 1) {
                                    tmp2['first'] = tmp2['last'] = ocr_list[i]['video_list'].shift();
                                } else {
                                    tmp2['first'] = ocr_list[i]['video_list'].shift();
                                    tmp2['last'] = ocr_list[i]['video_list'].pop();

                                }
                                tmp2['title'] = ocr_list[i]['text'];
                                tmp.push(tmp2);
                            }
                        }

                        if (tmp != '') {
                            if ($("#ocr").hasClass("disabled")) {
                                $("#ocr").removeClass("disabled");
                            }

                            g_ocrList = tmp;
                        }
                        g_mutex = 0;
                    }, 'json')
                }
            }
            function getAcr() {
                g_aMutex = 1;
                var tmp = [];
                if (settings.taskid && settings.isStart) {
                    $.get(dotData.acrUrl, {
                        'key' : settings.taskid,
                        'index' : 0,
                        'type' : 1,
                        'from_client' : true
                    }, function (data) {

                       var acr_list = data.list;
                        var len = acr_list.length;
                        var g_acrIndex = len;
                        if (len) {
                            for (var i in acr_list) {
                                if (acr_list[i]['text'] == '' || acr_list[i]['video_list'] == '')
                                    continue;

                                for (var j in acr_list[i]['video_list']) {
                                    acr_list[i]['video_list'][j] = acr_list[i]['video_list'][j] - 1;
                                }

                                var tmp2 = [];

                                if (acr_list[i]['video_list'].length == 1) {
                                    tmp2['first'] = tmp2['last'] = acr_list[i]['video_list'].shift();
                                } else {
                                    tmp2['first'] = acr_list[i]['video_list'].shift();
                                    tmp2['last'] = acr_list[i]['video_list'].pop();

                                }
                                tmp2['title'] = acr_list[i]['text'];
                                tmp.push(tmp2);
                            }
                        }

                        if (tmp != '') {
                            if ($("#acr").hasClass("disabled")) {
                                $("#acr").removeClass("disabled");
                            }

                            g_acrList = tmp;
                        }
                        g_aMutex = 0;
                    }, 'json')
                }
            }
            return {
                getData: getData,
                getSty2:getSty2,
                getOcr:getOcr,
                getAcr:getAcr
            }
        })()
    }
);